/**
 * Created by qintao30057 on 2017/3/10.
 */
//定义全局变量
var serviceQuestionGrid;//表格
var dhxWins,disposeWin; //窗口
var oneSQInfo; //一条管理员
var editStatus; //管理员状态
var searchNum = 0;
//页面布局
var pageObj = {type:"0100",height:[50,,35],paging:true,size:15}
var layoutContainer = create_layout(pageObj);
//初始化操作
$(function(){
    $("html,body").css({overflow:"hidden"});
    initGrid();
    $('#pagingArea').css({"height":"29px","bottom":"5px","left":"3px"});//分页
});
//初始化表格
function initGrid(){
    //获取表格对象
    serviceQuestionGrid = layoutContainer.grid[0];
    //定义表格表头
    var columnsInfo=[
        {header:'问题id',id:'id',width:60},
        {header:'来源',id:'service_source',width:120,type:'combo'},
        {header:'相关服务',id:'function_name',width:120},
        {header:'服务code',id:'function_code',hidden:true},
        {header:'所属医院',id:'hos_name',width:120 },
        {header:'医院code',id:'hos_code',hidden:true},
        {header:'反馈内容',id:'content',width:'*' },
        {header:'用户id',id:'alipay_id',width:120 },
        {header:'联系方式',id:'telephone',width:120 },
        {header:'反馈时间',id:'gmt_create',width:125 },
        {header:'问题状态',id:'status',hidden:true,type:'combo'},
        {header:'处理时间',id:'gmt_dispose',hidden:true},
        {header:'处理意见',id:'dispose_result',hidden:true},
        {header:'处理人',id:'dispose_operator',hidden:true},
        {header:'备注',id:'remark',hidden:true}
    ];
    //创建表格
    dmjplugs.grid.createGrid({ sm:'ch',  skin:'dhx_skyblue', srcc:true,  columns:columnsInfo },serviceQuestionGrid);
    serviceQuestionGrid.setTotal(0);
    serviceQuestionGrid.getColumnCombo(serviceQuestionGrid.getColIndexById('status')).addOption(serviceStatusComboData);
    serviceQuestionGrid.getColumnCombo(serviceQuestionGrid.getColIndexById('service_source')).addOption(serviceSourceComboData);
    serviceQuestionGrid.getColumnCombo(serviceQuestionGrid.getColIndexById('status')).deleteOption("");
    serviceQuestionGrid.getColumnCombo(serviceQuestionGrid.getColIndexById('service_source')).deleteOption("");
    serviceQuestionGrid.enableEditEvents(false,false,false);
    //注册分页事件
    serviceQuestionGrid.attachEvent("loadPagingData", function(first, limit){
        findPageGrid(first, limit);
    });

    serviceQuestionGrid.attachEvent("onRowDblClicked",function () {
        doDispose();
    });
    //隐藏掉复选框
    serviceQuestionGrid.setColumnHidden(0,true);

    //获取表格数据
    findPageGrid();

}
//根据选择条件,获取表格数据
function findPageGrid(first,limit){
    //初始化表格为空表
    if (!limit) {
        first = 0;
        limit = serviceQuestionGrid.getPageRowLimit();
        serviceQuestionGrid.clearAll();
        serviceQuestionGrid.setTotal(0);
    }
    //分页组件数据
    var postData="pageNo="+parseInt(first/limit+1)+"&pageSize="+limit+"&pageCount="+serviceQuestionGrid.getPageTotal();
    //查询条件
    if (typeof (search_Form) == 'object') {
        var formData = search_Form.getFormData();//获取查询表单数据
        var serviceSource = $.trim(formData.serviceSource);
        var functionCode = $.trim(formData.functionCode);
        var hosCode = $.trim(formData.hosCode);
        var beginTime = $.trim(search_Form.getInput("beginTime").value);
        var endTime = $.trim(search_Form.getInput("endTime").value);
        if (serviceSource != '') {
            postData += "&serviceSource=" + serviceSource;
        }
        if (functionCode != '') {
            postData += "&functionCode=" + functionCode;
        }
        if (hosCode != '') {
            postData += "&hosCode=" + hosCode;
        }
        if (beginTime.length > 0) {
            postData += "&beginTime=" + beginTime;
        }
        if (endTime.length > 0) {
            postData += "&endTime=" + endTime;
        }
    }
    $.ajax({
        url: basePath+'serviceQuestion/pageFindSQGrid',
        type: 'post',
        dataType: 'json', //返回的数据类型
        data:encodeURI(postData),
        success: function (data) {
            if(data&&data.rowCount>0){
                serviceQuestionGrid.parse(data.gridr, "json");
                serviceQuestionGrid.setTotal(data.rowCount);
                serviceQuestionGrid.uncheckAll();
                serviceQuestionGrid.forEachRow(function(id){
                    var functionCode = $.trim(serviceQuestionGrid.cells(id, serviceQuestionGrid.getColIndexById('function_code')).getValue());
                    if (functionCode == '-1') {
                        serviceQuestionGrid.cells(id, serviceQuestionGrid.getColIndexById('function_name')).setValue("其他");
                    }
                });
            }
        }
    });
}
//------------------------------------------------顶部操作区----------------------------------------------------------------------
// 顶部查询按钮	注册点击事件
function doOnButtFormInit() {
    if (typeof (butt_Form) == 'object') {
        butt_Form.attachEvent("onButtonClick",function(name,command) {
            if (command == 'dispose')//新增
                doDispose();
            else if (command == 'export')//详情
                doExport(command);
            else if (command == 'delete')//删除
                doDelete();
            else if (command == 'refresh')//刷新
                doRefresh();
        });
    }
}

//顶部查询框初始化
function doOnSearchFormInit(){
    if (typeof(search_Form) != 'object') return;
    moduleSearchInit();

    search_Form.attachEvent("onButtonClick", function(name, command) {
        if (command == 'search')
            doRefresh();
        else if(command == 'reset')
            doReset();
    });

    //来源
    var serviceSource_combo = search_Form.getCombo('serviceSource');
    serviceSource_combo.addOption(serviceSourceComboData);
    serviceSource_combo.setComboValue('');
    serviceSource_combo.readonly(true);
    serviceSource_combo.attachEvent("onChange", function () {
        doRefresh();
    });

    //服务项
    var functionCode_combo = search_Form.getCombo('functionCode');
    functionCode_combo.attachEvent("onChange", function () {
        doRefresh();
    });

    //医院
    var hosCode_combo = search_Form.getCombo('hosCode');
    hosCode_combo.addOption(hosComboData);
    hosCode_combo.setComboValue('');
    hosCode_combo.readonly(true);

    hosCode_combo.attachEvent("onChange", function () {
        var hosCode = $.trim(hosCode_combo.getSelectedValue());
        if (!hosCode || hosCode == '') {
            functionCode_combo.setComboValue('');
            functionCode_combo.clearAll(true);
            functionCode_combo.unSelectOption();
        } else {
            findHosFunc(hosCode);
        }
    });

    //开始时间
    var beginTimeCalendar = search_Form.getCalendar('beginTime');
    beginTimeCalendar.setDateFormat("%Y-%m-%d");
    beginTimeCalendar.showTime();
    //结束时间
    var endTimeCalendar = search_Form.getCalendar('endTime');
    endTimeCalendar.setDateFormat("%Y-%m-%d");
    endTimeCalendar.showTime();
    //限制日期输入
    $("input[name='beginTime']").keyup(function(){
        var tmptxt=$(this).val();
        if(validateDate(tmptxt)){
            $(this).val(tmptxt.replace(/[^0-9- :]/g,''));
        }

    }).change(function(){
        var tmptxt=$(this).val();
        if(validateDate(tmptxt)){
            tmptxt=tmptxt.replace(/[^0-9- :]/g,'');
        }
        $(this).val(tmptxt);
        if(!checkDateStr('yyyy-MM-dd hh:mm:ss',tmptxt)){
            $func.alert("筛选条件评价时间起始录入不规范！");
            dateFlag=false;
        }else{
            dateFlag=true;
        }
    }).css("ime-mode", "disabled").attr('placeholder','yyyy-MM-dd').attr("maxlength",10);
    $("input[name='endTime']").keyup(function(){
        var tmptxt=$(this).val();
        if(validateDate(tmptxt)){
            $(this).val(tmptxt.replace(/[^0-9- :]/g,''));
        }
    }).change(function(){
        var tmptxt=$(this).val();
        if(validateDate(tmptxt)){
            tmptxt=tmptxt.replace(/[^0-9- :]/g,'');
        }
        $(this).val(tmptxt);
        if(!checkDateStr('yyyy-MM-dd hh:mm:ss',tmptxt)){
            $func.alert("筛选条件评价时间截止录入不规范！");
            dateFlag1=false;
        }else{
            dateFlag1=true;
        }
    }).css("ime-mode", "disabled").attr('placeholder','yyyy-MM-dd').attr("maxlength",10);

    $('input[name="beginTime"]').keypress(function(event){
        if(event.keyCode == 13){ //绑定回车
            doRefresh();
        }
    });
    $('input[name="endTime"]').keypress(function(event){
        if(event.keyCode == 13){ //绑定回车
            doRefresh();
        }
    });
}

function findHosFunc(hosCode) {
    var postData = 'hosCode=' + hosCode;
    $.ajax({
        url: basePath + 'serviceQuestion/getHosFunc',
        type: 'post',
        dataType: 'json',
        data:encodeURI(postData),
        success: function (back) {
            if(!back||!back.result){
                $func.alert(back.message, "red");
                return;
            }
            if(back.content){
                var comboData = eval(back.content);
                var functionCodeCombo = search_Form.getCombo('functionCode');
                functionCodeCombo.clearAll();
                functionCodeCombo.addOption(comboData);
                functionCodeCombo.setComboValue('');
            }
        }
    });
}

//刷新按钮
function doRefresh(){
    findPageGrid();
}

function doReset(){
    var beginTimeCalendar = search_Form.getInput('beginTime');
    beginTimeCalendar.value = "";
    var endTimeCalendar = search_Form.getInput('endTime');
    endTimeCalendar.value = "";
    search_Form.setFormData({
        serviceSource:'',
        functionCode:'',
        hosCode:'',
    });
}
//--------------------------------------------------事件方法--------------------------------------------------------------------------
//处理
function doDispose(){
    var questions = getCheckedRowsByGrid(serviceQuestionGrid, serviceQuestionGrid.getColIndexById('select'));
    if (!questions || questions.length == 0 || questions.length > 1) {
        $func.alert('请选择一行再操作', 'red');
        return;
    }
    oneSQInfo = questions[0];
    if(!dhxWins) dhxWins = new dhtmlXWindows();
    dhxWins.setImagePath(basePP + "js/dhtmlx/dhtmlxWindows/codebase/imgs/");
    disposeWin = dhxWins.createWindow("disposeWin", 10, 10, 600, 450);
    disposeWin.center();
    disposeWin.setModal(true);
    disposeWin.bringToTop();
    disposeWin.setText("<span style=\"font-size:14px;\">"+"处理"+"</span>");
    disposeWin.denyResize();
    disposeWin.button("minmax1").hide();
    disposeWin.button("park").hide();
    disposeWin.attachURL(basePath+"serviceQuestion/gotoDisposeSQWin");
}

//删除
function doDelete(){
    var questions = getCheckedRowsByGrid(serviceQuestionGrid, serviceQuestionGrid.getColIndexById('select'));
    if (questions == null||questions.length==0) {
        $func.alert("至少选择一行再操作[删除]", "red");
        return;
    }
    if (questions.length>1) {
        $func.alert("不支持多行删除，请选择一行再操作[删除]", "red");
        return;
    }
    checkParam(questions[0].id);
    var confirmArr = "确定删除此问题？ ";
    var postData="id="+questions[0].id;
    $func.confirm({
        content : confirmArr,
        ok : function() {
            $.ajax({
                url: basePath+ 'serviceQuestion/deleteSQ',
                type: 'post',
                dataType: 'json',
                data:postData,
                success: function (back) {
                    if(!back||!back.result){
                        $func.alert("删除失败！","red");
                        return;
                    }
                    $func.alert("删除成功！","green");
                    doRefresh();
                }
            });
        }
    });
}

//导出
function doExport(command) {
    if (command != 'export') return;
    $('#exportForm').submit();
}

